package impl

import (
	"context"
	compb "gitee.com/micro-services/protocol/common"
	pb "gitee.com/micro-services/protocol/room/server"
	"micro.room.server/do"
	"micro.room.server/models"
	"time"
)

func CreateRoomDevice(ctx context.Context, r *pb.CreateRoomDeviceReq) (*pb.CreateRoomDeviceRsp, error) {
	err := do.CreateRoomDevice(&models.VsRDevice{
		RoomId:       r.Info.RoomID,
		BedNum:       r.Info.BedNum,
		BedImage:     r.Info.BedImage,
		BedAttribute: r.Info.BedAttribute,
		PeopleNum:    r.Info.PeopleNum,
		HasNetwork:   r.Info.HasNetWork,
		HasWifi:      r.Info.HasWifi,
		HasTv:        r.Info.HasTV,
		HasTel:       r.Info.HasTel,
		HasBreakfast: r.Info.HasBreakfast,
		NoticeId:     r.Info.NoticeID,
		CreatedTime:  time.Now().Unix(),
		UpdatedTime:  time.Now().Unix(),
	})
	if err != nil {
		return nil, err
	}

	return &pb.CreateRoomDeviceRsp{
		RetCode: 0,
		Msg:     Success,
	}, nil
}

func UpdateRoomDeviceInfo(ctx context.Context, r *pb.UpdateRoomDeviceInfoReq) (*pb.UpdateRoomDeviceInfoRsp, error) {
	err := do.UpdateRoomDeviceInfo(&models.VsRDevice{
		Id:           r.DeviceID,
		BedNum:       r.BedNum,
		BedImage:     r.BedImage,
		BedAttribute: r.BedAttribute,
		PeopleNum:    r.PeopleNum,
		HasNetwork:   r.HasNetWork,
		HasWifi:      r.HasWifi,
		HasTv:        r.HasTV,
		HasTel:       r.HasTel,
		HasBreakfast: r.HasBreakfast,
		NoticeId:     r.NoticeID,
		UpdatedTime:  time.Now().Unix(),
	})
	if err != nil {
		return nil, err
	}

	return &pb.UpdateRoomDeviceInfoRsp{
		RetCode: 0,
		Msg:     Success,
	}, nil
}

func DeleteRoomDevice(ctx context.Context, r *pb.DeleteRoomDeviceReq) (*pb.DeleteRoomDeviceRsp, error) {
	err := do.DeleteRoomDevice(r.DeviceID)
	if err != nil {
		return nil, err
	}

	return &pb.DeleteRoomDeviceRsp{
		RetCode: 0,
		Msg:     Success,
	}, nil
}

func SearchRoomDevice(ctx context.Context, r *pb.SearchRoomDeviceReq) ([]*compb.RoomDeviceInfo, int64, error) {
	devices, count, err := do.SearchRoomDevice(&models.SearchRoomDeviceParams{
		RoomNo:    r.RoomNo,
		BedNum:    r.BedNum,
		PeopleNum: r.PeopleNum,
		Offset:    r.Offset,
		Limit:     r.Limit,
	})
	if err != nil {
		return nil, 0, err
	}

	infos := make([]*compb.RoomDeviceInfo, 0)
	for _, v := range devices {
		infos = append(infos, &compb.RoomDeviceInfo{
			DeviceID:      v.Id,
			RoomID:        v.RoomId,
			BedNum:        v.BedNum,
			BedImage:      v.BedImage,
			BedAttribute:  v.BedAttribute,
			PeopleNum:     v.PeopleNum,
			HasNetWork:    v.HasNetwork,
			HasWifi:       v.HasWifi,
			HasTV:         v.HasTv,
			HasTel:        v.HasTel,
			HasBreakfast:  v.HasBreakfast,
			NoticeID:      v.NoticeId,
			NoticeContent: "",
		})
	}

	return infos, count, nil
}

func DeviceList(ctx context.Context, r *pb.DeviceListReq) ([]*compb.RoomDeviceInfo, error) {
	devices, err := do.DeviceList(r.RoomID)
	if err != nil {
		return nil, err
	}

	infos := make([]*compb.RoomDeviceInfo, 0)

	for _, v := range devices {
		infos = append(infos, &compb.RoomDeviceInfo{
			DeviceID:      v.Id,
			RoomID:        v.RoomId,
			BedNum:        v.BedNum,
			BedImage:      v.BedImage,
			BedAttribute:  v.BedAttribute,
			PeopleNum:     v.PeopleNum,
			HasNetWork:    v.HasNetwork,
			HasWifi:       v.HasWifi,
			HasTV:         v.HasTv,
			HasTel:        v.HasTel,
			HasBreakfast:  v.HasBreakfast,
			NoticeID:      v.NoticeId,
			NoticeContent: "",
		})
	}

	return infos, nil
}
